﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using JetBrains.Annotations;

namespace WikiDoc.Lines
{
    /// <summary>
    /// 数字列表
    /// </summary>
    class NumberListLine : ListLine
    {
        protected NumberListLine(string line, int level, int level2, string content) 
            : base(line, LineTypes.NumberList, level, level2, content)
        {
        }

        /// <summary>
        /// 数字列表行定义：[至少一个空格] [数字] [.、空格][若干空格] 内容
        /// </summary>
        public static readonly Regex Pattern = new Regex(
            @"\A (\s+)  ([0-9]+ [\.、\s]\s*)   (.*)",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant |
            RegexOptions.Singleline |
            RegexOptions.IgnorePatternWhitespace);

        /// <summary>
        /// 如果给定的行是一个数字列表行，则返回解析出的信息，否则返回 null
        /// </summary>
        /// <param name="line">The line.</param>
        /// <returns></returns>
        [CanBeNull]
        public static WikiLine TryParse([CanBeNull] string line)
        {
            if (line == null)
            {
                return null;
            }

            var match = Pattern.Match(line);

            return match.Success
                       ? new NumberListLine(line,
                                      match.Groups[1].Length,
                                      match.Groups[1].Length + match.Groups[2].Length,
                                      match.Groups[3].Value)
                       : null;
        }
    }
}
